package com.hsy.order.base.ui.widget

import android.content.Context
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.TextView
import com.flux.utils.ToastUtil
import com.hsy.order.R

/**
 * Created on 2018/5/22.
 *
 * @author Yang
 */
class InputDialog(context: Context, onConfirmClickListener: (String) -> Unit) {

    private val dialog: AlertDialog
    private val input: EditText
    private val cancel: TextView
    private val confirm: TextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_input, null)
        dialog = AlertDialog.Builder(context)
                .setView(view)
                .setCancelable(false)
                .create()
        input = view.findViewById(R.id.dialog_input)
        cancel = view.findViewById(R.id.dialog_cancel)
        confirm = view.findViewById(R.id.dialog_confirm)

        cancel.setOnClickListener { dialog.cancel() }
        confirm.setOnClickListener {
            val text = input.text.toString()
            if (text.isEmpty()) {
                ToastUtil.showToast(context, "内容不能为空")
            } else {
                onConfirmClickListener(text)
                dialog.cancel()
            }
        }
    }

    fun setInputType(type: Int) {
        input.inputType = type
    }

    fun setHint(hint: String) {
        input.hint = hint
    }

    fun show() {
        dialog.show()
    }
}